﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using ApplicationLibrary.CounterManager;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using Plumber.Masters.ModuleData;
using System.Resources;

namespace Plumber.Accounting.Forms
{
    public partial class AccountingType : ApplicationLibrary.DBManager.DocumentForm
    {
        private EnumsManager<CodeMode> eCodeMode = null;
        private AccountingTypeEnumManager eMasterType = null;
        private EnumsManager<AccountType> eAccountType = null;
        private DUCodes duCodes = null;
        private ComboBoxManager cbbTaxCodeManager = null;
        private ComboBoxManager cbbRestraintManager = null;
        private RRTaxCode rRTaxCode = null;

        public AccountingType()
            : base(Properties.Resources.FN_MasterType)
        {
            InitializeComponent();
            keyControl = txtCode;
        }

        #region Virtual Method

        protected override IRadarParameters GetKeyFromDocument()
        {
            return new RadarMasterTypesParam(txtCode.Text);
        }

        protected override void OnAttachData()
        {
            dbManager = new dbmanagerAccountingType("mastertypeForm", new RadarMasterTypes(true), this);
            dbManager.AddMaster(this, PL_MasterType.Name, true);

            eCodeMode = new EnumsManager<CodeMode>(cbbCodeMode);
            eMasterType = new AccountingTypeEnumManager(cbbMasterType);
            eAccountType = new EnumsManager<AccountType>(cbbAccountType);

            rRTaxCode = new RRTaxCode();
            cbbTaxCodeManager = new ComboBoxManager();
            cbbRestraintManager = new ComboBoxManager();
            cbbTaxCodeManager.AddValue(string.Empty, string.Empty);
            cbbRestraintManager.AddValue(string.Empty, string.Empty);

            duCodes = new DUCodes(false);
        }

        protected override void OnBindData()
        {
            BindControl(txtCode, PL_MasterType.Code);
            BindControl(txtDescription, PL_MasterType.Description);
            BindControl(cbbMasterType, PL_MasterType.MasterType, "SelectedValue", NullValue.NotSet);
            BindControl(cbbCodeMode, PL_MasterType.CodeMode, "SelectedValue", NullValue.NotSet);
            BindControl(cbbAccountType, PL_MasterType.AccountType, "SelectedValue", NullValue.NotSet);
            BindControl(ckbDocNoReq, PL_MasterType.DocNoRequired, "Checked");
            BindControl(ckbCopyDocNo, PL_MasterType.CopyDocNo, "Checked");
            BindControl(ckbIsPrefer, PL_MasterType.IsPrefer, "Checked");
        }

        protected override void FocusOnNew()
        {
            txtCode.Focus();
        }

        protected override void OnDisableControlsForEdit()
        {
            txtCode.Enabled = false;
        }

        protected override void OnDisableControlsForNew()
        {
        }

        protected override bool OnBeforeSave()
        {
            if (eCodeMode.GetValue() != CodeMode.E_IsEmpty && !duCodes.Find(txtCode.Text))
            {
                CodesManager cm = new CodesManager();
                cm.AddCode(txtCode.Text, txtDescription.Text);
            }

            return base.OnBeforeSave();
        }

        protected override bool OnBeforeDelete()
        {
            if (!base.OnBeforeDelete())
                return false;

            if (duCodes.Find(txtCode.Text))
            {
                CodesManager cm = new CodesManager();
                cm.DeleteCode(txtCode.Text);
            }

            return true;
        }

        #endregion

        private void cbbMasterType_SelectedValueChanged(object sender, EventArgs e)
        {
            OnDisableControlsForEdit();
        }
    }

    #region DbManager dbmanagerAccountingType

    /// <summary>
    /// Classe derivata per gestioni particolari
    /// </summary>
    internal class dbmanagerAccountingType : DBManager
    {
        public dbmanagerAccountingType(string name, RadarForm radar, DocumentForm document)
            : base(name, radar, document)
        { }

        protected override void dAdapter_MasterRowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            if (e.StatementType == StatementType.Insert)
            {
                if (e.Row[PL_MasterType.IsAccountingType.Name] == System.DBNull.Value)
                    e.Row[PL_MasterType.IsAccountingType.Name] = 1;
            }
        }

        protected override string CreateMasterQuery()
        {
            this.Table = new PL_MasterType();
            QueryBuilder qB = new QueryBuilder();
            qB.AddSelect();
            qB.AddFrom(this.Table);
            qB.AddCompare(PL_MasterType.Code, "@p1");

            return qB.Query;
        }

        protected override List<SqlABParameter> CreateMasterParam()
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();

            SqlABParameter sParam = new SqlABParameter("@p1", PL_MasterType.Code);
            sParam.Value = PL_MasterType.Code.DefaultValue;
            PList.Add(sParam);
            return PList;
        }

        protected override void SetParameters(IRadarParameters key, DBCollection collection)
        {
            collection.Parameter[0].Value = key[0];
        }
    }

    #endregion

    internal class AccountingTypeEnumManager : EnumsManager<MasterTypes>
    {
        public AccountingTypeEnumManager(ComboBox cbbBox)
            : base(cbbBox)
        {
        }

        public override bool DisplayValue(MasterTypes t)
        {
            switch (t)
            {
                case MasterTypes.E_BankCash:
                case MasterTypes.E_PurchaseInvoice:
                case MasterTypes.E_SaleInvoice:
                case MasterTypes.E_Tax:
                case MasterTypes.E_Other:
                case MasterTypes.E_Deduction:
                case MasterTypes.E_Restraint:
                    return true;
                default:
                    return false;
            }
        }
    }
}